<?php


namespace App\Http\Controllers;


class AdminController extends Controller
{
    protected $faild_code = [
        1   => '参数错误',
        2   => '未携带token',
        3   => '用户不存在',
        5   => '密码在6到20位，两次密码要一致。',
        6   => '该角色下有成员，不能删除。',

        1001    => 'token已过期',
        1002    => 'token已刷新',
    ];

    protected $error_code = [
        1   => '帐号或密码错误',
        2   => '验证码错误',
        3   => '无权操作',
    ];

    /**
     * 获取用户身份
     * @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|\Illuminate\Contracts\Foundation\Application
     */
    protected function auth()
    {
        return auth('admin');
    }


    /**
     * 返回成功信息json
     * @param array $data
     * @param int $code
     * @return mixed|\Symfony\Component\HttpFoundation\ParameterBag|null
     */
    protected function ReturnSuccessInfo($data = [], int $code = 0)
    {
        $data = collect($data)->toArray();

        return response()->json([
            'code'  => $code,
            'msg'   => 'ok',
            'data'  => $data,
        ]);
    }

    /**
     * 返回数据验证错误json
     * @param int $code
     * @param array $data
     * @return mixed|\Symfony\Component\HttpFoundation\ParameterBag|null
     */
    protected function ReturnErrorInfo(int $code , $data = [])
    {
        $data = collect($data)->toArray();

        return response()->json([
            'code'  => $code,
            'msg'   => $this->error_code[$code],
            'data'  => $data,
        ]);
    }

    /**
     * 返回分页数据
     * @param array $list
     * @return mixed|\Symfony\Component\HttpFoundation\ParameterBag|null
     */
    protected function ReturnPage($list = [])
    {

        $data = [
            'code' => 0,
            'msg'   =>'',
            'data'  => $list->items(),
            'page'  => $list->currentPage(),
            'total_page' => $list->lastPage(),
            'num'   => $list->count(),
            'count' => $list->total(),
        ];

        return response()->json($data);
    }

    /**
     * 返回错误json
     * @param int $code
     * @param array $data
     * @return mixed|\Symfony\Component\HttpFoundation\ParameterBag|null
     */
    protected function ReturnFailed(int $code , $data = [])
    {
        return response()->json([
            'code'  => $code,
            'msg'   => $this->faild_code[$code],
            'data'  => $data
        ]);
    }

}
